package com.wlyy.test.redis;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.transaction.annotation.Transactional;

import com.wlyy.bcwlw.common.cache.CacheConst;
import com.wlyy.bcwlw.sys.application.dao.ApplicationDAO;
import com.wlyy.bcwlw.sys.application.entity.ApplicationDTO;
import com.wlyy.bcwlw.sys.menu.dao.MenuDAO;
import com.wlyy.bcwlw.sys.menu.dao.ModuleDAO;
import com.wlyy.bcwlw.sys.menu.entity.MenuDTO;
import com.wlyy.bcwlw.sys.menu.entity.ModuleDTO;
import com.wlyy.bcwlw.sys.permission.dao.PermissionDAO;
import com.wlyy.bcwlw.sys.permission.entity.PermissionDTO;
import com.wlyy.bcwlw.sys.role.dao.RoleDAO;
import com.wlyy.bcwlw.sys.role.entity.RoleDTO;
import com.wlyy.bcwlw.sys.user.dao.UserDAO;
import com.wlyy.bcwlw.sys.user.entity.UserDTO;
import com.wlyy.test.base.BaseSpringTestCase;

public class InitRedisData extends BaseSpringTestCase{

	@Autowired
	private RedisTemplate<String,Object> redisHashTemplate;
	
	
	@Autowired
	private RoleDAO roleDAO;
	@Autowired
	private MenuDAO menuDAO;
	@Autowired
	private PermissionDAO permissionDAO;
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private ModuleDAO moduleDAO;
	@Autowired
	private ApplicationDAO applicationDTO;
	
	
	
	@Transactional
	public void setDataInit(){
		redisHashTemplate.execute(new SessionCallback<List<Object>>(){
			@Override
			public  List<Object> execute(RedisOperations operations) throws DataAccessException {
				// TODO Auto-generated method stub
				operations.multi();
				
				operations.delete(CacheConst.ROLE_KEY);
				HashOperations<String,String, RoleDTO> roleHash = operations.opsForHash();
				List<RoleDTO> roleList = roleDAO.queryByList(null);
				for(RoleDTO roleDTO:roleList){
					roleHash.put(CacheConst.ROLE_KEY, String.format("%s:Object",roleDTO.getId()), roleDTO);
				}
				
				operations.delete(CacheConst.MENU_KEY);
				HashOperations<String,String, MenuDTO> menuHash = operations.opsForHash();
				List<MenuDTO> menuList = menuDAO.queryByList(null);
				for(MenuDTO menuDTO:menuList){
					menuHash.put(CacheConst.MENU_KEY, String.format("%s:Object",menuDTO.getId()), menuDTO);
				}
				
				operations.delete(CacheConst.PERMISSION_KEY);
				HashOperations<String,String, PermissionDTO> permissionDTOHash = operations.opsForHash();
				List<PermissionDTO> permissionList = permissionDAO.queryByList(null);
				for(PermissionDTO permissionDTO:permissionList){
					permissionDTOHash.put(CacheConst.PERMISSION_KEY, String.format("%s:Object",permissionDTO.getId()), permissionDTO);
				}
				
				operations.delete(CacheConst.USER_KEY);
				HashOperations<String,String, UserDTO> userHash = operations.opsForHash();
				List<UserDTO> userList = userDAO.queryByList(null);
				for(UserDTO userDTO:userList){
					userHash.put(CacheConst.USER_KEY, String.format("%s:Object",userDTO.getUserId()), userDTO);
				}
				
				operations.delete(CacheConst.MODULE_KEY);
				HashOperations<String,String, ModuleDTO> moduleHash = operations.opsForHash();
				List<ModuleDTO> moduleList = moduleDAO.queryByList(null);
				for(ModuleDTO moduleDTO:moduleList){
					moduleHash.put(CacheConst.MODULE_KEY, String.format("%s:Object",moduleDTO.getId()), moduleDTO);
				}
				
				operations.delete(CacheConst.APPLICATION_KEY);
				HashOperations<String,String, ApplicationDTO> applicationHash = operations.opsForHash();
				List<ApplicationDTO> applicationList = applicationDTO.queryByList(null);
				for(ApplicationDTO applicationDTO:applicationList){
					applicationHash.put(CacheConst.APPLICATION_KEY, String.format("%s:Object",applicationDTO.getAppId()), applicationDTO);
				}
				
				
				return operations.exec();
			}
			});
////				

//				return operations.exec();
//			}
//		});
		
	}
	
	@Test
	public void setUstkd(){
		
		HashOperations<String,String, List<UserDTO>> objHash = redisHashTemplate.opsForHash();
		List<UserDTO> list = objHash.get("TEST", "list");
		for(UserDTO user:list){
			System.out.println(user.getName());
		}
	}
	
	
}
